import { defineStore } from 'pinia';
import { getToken, removeToken, setToken } from '../utils/token-utils';
import type { UserInfoState } from './interface';
import {ElMessage} from 'element-plus'
import {staticRoutes} from '@/router/routes'
import { getUserInfoApi, loginApi,logoutApi } from '@/api/acl/login';

/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfoStore = defineStore('userInfo', {

  
	state: (): UserInfoState => ({
    token: getToken() as string,
    name: '',
    avatar: '',
    menuRoutes: [],
    buttons: [],
    roles:[],
    routesName:[],
 
  }),


  
  actions: {
    // 登陆功能
    async login (username: string, password: string) {
     const result = await loginApi(username,password)
     setToken(result.token)
     this.token = result.token
    },

    // 获取用户信息 用户昵称 头像
    async getInfo () {
      // return new Promise((resolve, reject) => {
      //   setTimeout(() => {
      //     this.name = 'admin'
      //     this.avatar = 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'
      //     this.menuRoutes = staticRoutes
      //     resolve({name: this.name, avatar: this.avatar, token: this.token})
      //   }, 1000)
      // })

      const result = await getUserInfoApi()
      this.name = result.name
      this.avatar = result.avatar
      this.buttons = result.buttons //按钮权限会用
      this.roles = result.roles //涉及到角色全选
      this.routesName = result.routes //涉及路由权限
      this.menuRoutes = staticRoutes
    },

    // 重置 
    reset () {
      // 删除local中保存的token
      removeToken()
      // 提交重置用户信息的mutation
      this.token = ''
      this.name = ''
      this.avatar = ''
    },
	},
});
